package com.example.lianjuan.ui.adapter;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.example.lianjuan.Base.IBaseInfo;
import com.example.lianjuan.Model.domain.HomePagerCategories;
import com.example.lianjuan.R;
import com.example.lianjuan.Utils.LogUtils;
import com.example.lianjuan.Utils.UtilsUrl;

import java.util.ArrayList;
import java.util.List;

public class looperPagerAdapter extends RecyclerView.Adapter<looperPagerAdapter.InnerHolder> {
    private List<HomePagerCategories.DataDTO> looperData = new ArrayList<>();
    private OnLooperItemClickLister onLooperItemClickLister = null;

    public void setData(List<HomePagerCategories.DataDTO> data) {
        looperData.clear();
        looperData.addAll(data);
        notifyDataSetChanged();
    }

    public int getSize() {
        return looperData.size();
    }

    @NonNull
    @Override
    public InnerHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View inflate = LayoutInflater.from(parent.getContext()).inflate(R.layout.looper_pager, parent, false);
        return new InnerHolder(inflate);
    }

    @Override
    public void onBindViewHolder(@NonNull InnerHolder holder, int position) {
        //越界处理
        if (looperData.size() != 0) {
            int realPosition = position % looperData.size();
            HomePagerCategories.DataDTO dataDTO = looperData.get(realPosition);
            holder.setData(dataDTO);
            holder.looperImager.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (onLooperItemClickLister != null) {
                        onLooperItemClickLister.onLooperItemClick(dataDTO);
                    }
                }
            });
        }

    }

    @Override
    public int getItemCount() {
        return Integer.MAX_VALUE;
    }

    public class InnerHolder extends RecyclerView.ViewHolder {
        private ImageView looperImager;

        public InnerHolder(@NonNull View itemView) {
            super(itemView);
            looperImager = itemView.findViewById(R.id.looper_imager);
        }

        public void setData(HomePagerCategories.DataDTO dataDTO) {
            ViewGroup.LayoutParams layoutParams = itemView.getLayoutParams();
            Glide.with(itemView.getContext()).load(UtilsUrl.getCoverPath(dataDTO.getPict_url(), 300)).into(looperImager);
        }
    }

    public void setLooperItemClickLister(OnLooperItemClickLister onLooperItemClickLister) {
        this.onLooperItemClickLister = onLooperItemClickLister;
    }

    public interface OnLooperItemClickLister {
        void onLooperItemClick(IBaseInfo iBaseInfo);
    }
}
